<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-12
 * Time: 9:09
 */

namespace app\common\logic;
use app\common\logic\Logic;
use app\common\model\FareTmpl;
use app\common\model\CarryMode;
use app\common\model\InclPostageProviso;
use app\common\model\Group;
use app\common\model\Favorite;
use app\common\model\Score;
use app\common\model\Goods;

class GoodsLogic extends Logic
{
    protected $modelClass = 'app\common\model\Goods';
//    protected $with = 'content';
    protected $column = 'id,title,type_name,type_id,price,user_id,co_type_id,status';
    

    
    
    public function getFare($fareId){
        
        $fare = FareTmpl::get($fareId);
        $carry = CarryMode::all(['FareId'=>$fareId,'IsDefault'=>0]);
        $carry_default = CarryMode::get(['FareId'=>$fareId,'IsDefault'=>1]);
        $proviso = InclPostageProviso::get(['FareId'=>$fareId]);

        return [
            'isProviso'=>$fare->IsInclPostage,
            'carry_default'=>$carry_default,
            'carry'=>$carry,
            'minAmount'=>$proviso?$proviso->Amount:-1//证明没有满包邮的活动
        ];
    }
    
   
    
    public function getFavorite($goods_id,$user_id){
        $rt = false;
        $isHas = Favorite::get(['user_id'=>$user_id,'goods_id'=>$goods_id]);
        if($isHas) $rt =true;
        return $rt;
    }
    
    public function changeGoodsStatus($goodsId,$status){
        $goods = $this->findModel($goodsId);
        $goods->status = $status;
        $goods->isUpdate()->save();
        return true;
    }
    
    public function getGroups($goodsId){
        //获取通过审核的而且上架的团购，并拿出最近的一场。
        $group = Group::where(['goods_id'=>$goodsId,'validate'=>1,'status'=>3])->order('totime asc')->find();
        if(!$group){
            return null;
        }
        
        //已经转换过时间戳了
        $group->start_time = strtotime($group->totime);
        $group->end_time = strtotime($group->endtime);
        $group->now = time();
        return $group;
        
    }
    
    

    
    
    
    
}